package com.leyou.item.service.impl;

import com.leyou.common.enums.LyExceptionEnums;
import com.leyou.common.exception.LyException;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.entity.Stock;
import com.leyou.item.service.*;
import com.leyou.item.vo.SpuVO;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author linzx
 * @date 2020/3/25
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private StockService stockService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    @Transactional
    public void saveGoods(SpuVO spuVO) {
        Spu spu = new Spu();
        BeanUtils.copyProperties(spuVO,spu);
        if (spuVO.getId() == null) {
            spu.setCreateTime(new Date());
        }
        spu.setLastUpdateTime(new Date());
        spuService.insertSelective(spu);
        SpuDetail spuDetail = spuVO.getSpuDetail();
        spuDetail.setSpuId(spu.getId());
        spuDetailService.insertSelective(spuDetail);
        List<Stock> stockList = new ArrayList<>();
        for (Sku sku : spuVO.getSkus()) {
            sku.setSpuId(spu.getId());
            sku.setCreateTime(spu.getCreateTime());
            sku.setLastUpdateTime(spu.getCreateTime());
            skuService.insertSelective(sku);
            Stock stock = new Stock();
            stock.setSkuId(sku.getId());
            stock.setStock(sku.getStock());
            stockList.add(stock);
        }
        stockService.insertList(stockList);
        amqpTemplate.convertAndSend("item.insert",spu.getId());
    }

    /**
     * 先删除 在添加 就是更新
     * @param spuVO
     */
    @Override
    @Transactional
    public void updateGoods(SpuVO spuVO) {
        if (spuVO.getId() == null) {
            throw new LyException(LyExceptionEnums.INVALID_PARAM);
        }
        Date createTime = spuService.selectByPrimaryKey(spuVO.getId()).getCreateTime();
        deleteGoods(spuVO.getId());
        spuVO.setCreateTime(createTime);
        saveGoods(spuVO);
    }

    @Override
    @Transactional
    public void deleteGoods(Long spuId) {
        spuService.deleteByPrimaryKey(spuId);
        spuDetailService.deleteByPrimaryKey(spuId);
        Sku sku = new Sku();
        sku.setSpuId(spuId);
        List<Long> skuIds = skuService.select(sku).stream().map(Sku::getId).collect(Collectors.toList());
        skuService.deleteByIdList(skuIds);
        stockService.deleteByIdList(skuIds);
    }
}
